package com.leyou.gateway.filters;

import com.leyou.gateway.config.JwtProperties;
import com.leyou.gateway.task.PrivilegeTokenHolder;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

/**
 * @author Eric
 * @version v1.0
 * @package com.leyou.gateway.filters
 * @date 2019/7/10 19:44
 * @description:
 **/
@Component
public class PrivilegeFilter extends ZuulFilter {


    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private PrivilegeTokenHolder tokenHolder;


    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return FilterConstants.PRE_DECORATION_FILTER_ORDER + 1;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {

        //获取请求上下文
        RequestContext context = RequestContext.getCurrentContext();
        //将token存入Zuul请求头中
        context.addZuulRequestHeader(jwtProperties.getApp().getHeaderName(),tokenHolder.getToken());

        return null;
    }
}
